﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;
using Reborn_Server.App1AppHandler;

namespace Reborn_WorldServer.PendingTells
{
    public class TellRequestWS
    {
        public Character fromCharacter;
        public string toCharacterName;
        public string message;

        public string itemLinksXml = "<itemlinks/>";

        public string tellGUID;

        private Timer requestTimeout = new Timer(10000);

        public TellRequestWS()
        {
            requestTimeout.AutoReset = false;
            requestTimeout.Elapsed += new ElapsedEventHandler(requestTimeout_Elapsed);
            requestTimeout.Start();

            tellGUID = System.Guid.NewGuid().ToString();
        }

        //Pass the timeout back up to the parent manager so it can remove this from queue,
        //the manager will call process and send the failure message
        private void requestTimeout_Elapsed(object sender, ElapsedEventArgs e)
        {
            Managers.tellManager.tellRequestTimeout(this);
        }

        public void killTimeout()
        {
            try
            {
                requestTimeout.Stop();
                requestTimeout.Dispose();
            }
            catch { }
        }

        public void processResult(bool messageSuccessfull)
        {
            killTimeout();

            if (messageSuccessfull == false)
                fromCharacter.SendMessage("Server: Cannot find " + toCharacterName, "server");
            else
            {
                Message resultMessage = new Message();
                resultMessage.CreatureID = fromCharacter.CreatureID;
                resultMessage.Text = "You tell " + toCharacterName + ": " + message;
                resultMessage.Type = "tell";
                resultMessage.itemLinksXml = itemLinksXml;

                fromCharacter.AddMessage(resultMessage);
            }
        }
    }
}
